if(IS_ABSOLUTE "${INSTALL_BINDIR}")
  set(SZARP_BINDIR ${INSTALL_BINDIR})
else()
  set(SZARP_BINDIR ${CMAKE_INSTALL_PREFIX}/${INSTALL_BINDIR})
endif()

set(SZARP_LIBDIR ${default_prefix}/lib)
set(SZARP_PYLIBDIR ${default_prefix}/lib/python)

if(IS_ABSOLUTE ${INSTALL_CONFIGDIR})
  set(_configdir ${INSTALL_CONFIGDIR})
elseif(INSSTALL_CONFIG_DIR STREQUAL ".")
  set(_configdir ${INSTALL_CONFIGDIR})
else()
  set(_configdir ${CMAKE_INSTALL_PREFIX}/${INSTALL_CONFIGDIR})
endif()
set(SZARP_CONFIGDIR ${_configdir} CACHE STRING "Location of configuration files (config_folder)")
unset(_configdir)

if(IS_ABSOLUTE "${INSTALL_LOGDIR}")
  set(_logdir ${INSTALL_LOGDIR})
elseif(NOT INSTALL_LOGDIR)
  set(_logdir "/var/log/szarp/")
else()
  set(_logdir ${CMAKE_INSTALL_PREFIX}/${INSTALL_LOGDIR})
endif()
set(SZARP_LOGDIR ${_logdir} CACHE STRING "Location of log files; empty is console (logging_folder)")
unset(_logdir)

if(IS_ABSOLUTE "${INSTALL_SHAREDIR}")
  set(_sharedir ${INSTALL_SHAREDIR})
elseif(NOT INSTALL_SHAREDIR)
  set(_sharedir "/usr/share/szarp/")
else()
  set(_sharedir ${CMAKE_INSTALL_PREFIX}/${INSTALL_SHAREDIR})
endif()
set(SZARP_SHAREDIR ${_sharedir} CACHE STRING "Location share files such as PID file (share_folder)")
unset(_sharedir)

if(IS_ABSOLUTE "${INSTALL_RUNTIMEDIR}")
  set(_runtimedir ${INSTALL_RUNTIMEDIR})
elseif(NOT INSTALL_RUNTIMEDIR)
  set(_runtimedir "/var/run/szarp/")
else()
  set(_runtimedir ${CMAKE_INSTALL_PREFIX}/${INSTALL_RUNTIMEDIR})
endif()
set(SZARP_RUNTIMEDIR ${_runtimedir} CACHE STRING "Location runtime files such as PID file (runtime_folder)")
unset(_runtimedir)

if(IS_ABSOLUTE "${INSTALL_DATADIR}")
  set(_datadir ${INSTALL_DATADIR})
elseif(NOT INSTALL_DATADIR)
  set(_datadir "/var/lib/szarp/")
else()
  set(_datadir "${CMAKE_INSTALL_PREFIX}/${INSTALL_DATADIR}")
endif()
set(SZARP_DATADIR ${_datadir} CACHE STRING "Location of data files such as keyring file")
unset(_datadir)

include(TestSignalType)
include(TestBigEndian)
TEST_BIG_ENDIAN(SZARP_BIG_ENDIAN)

include(CheckFunctionExists)
CHECK_FUNCTION_EXISTS(alphasort HAVE_ALPHASORT)
CHECK_FUNCTION_EXISTS(asprintf HAVE_ASPRINTF)
CHECK_FUNCTION_EXISTS(gmtime_r HAVE_GMTIME_R)
CHECK_FUNCTION_EXISTS(localtime_r HAVE_LOCALTIME_R)
CHECK_FUNCTION_EXISTS(scandir HAVE_SCANDIR)
CHECK_FUNCTION_EXISTS(timegm HAVE_TIMEGM)

include(CheckIncludeFile)
CHECK_INCLUDE_FILE("libgen.h" HAVE_LIBGEN_H)
CHECK_INCLUDE_FILE("stdlib.h" HAVE_STDLIB_H)
CHECK_INCLUDE_FILE("string.h" HAVE_STRING_H)
CHECK_INCLUDE_FILE("sys/stat.h" HAVE_SYS_STAT_H)
CHECK_INCLUDE_FILE("sys/types.h" HAVE_SYS_TYPES_H)
CHECK_INCLUDE_FILE("unistd.h" HAVE_UNISTD_H)

find_package(Boost COMPONENTS system thread program_options regex filesystem python locale signals REQUIRED)
find_package(Protobuf REQUIRED)
find_package(FLEX REQUIRED)
if(${FLEX_VERSION} VERSION_GREATER "2.5.8")
  set(HAVE_YYLEX_DESTROY true)
endif()
find_package(CARES REQUIRED)
find_package(CRYPT REQUIRED)
find_package(OpenSSL REQUIRED)
find_package(XMLRPCEPI REQUIRED)
if(NOT ${XMLRPCEPI_FOUND})
  set(HAVE_XMLRPC_EPI_H true)
endif()
find_package(LibEvent REQUIRED)
find_package(ZeroMQ REQUIRED)
find_package(PythonInterp 2.7 EXACT REQUIRED)
find_package(PythonLibs 2.7 EXACT REQUIRED)
find_package(LibXml2 REQUIRED)
find_package(LibXslt REQUIRED)
find_package(CURL REQUIRED)
find_package(Systemd REQUIRED)

find_package(Zip REQUIRED)
if(${ZIP_FOUND})
	set(Zip_INCLUDE_DIRS ${ZIP_INCLUDE_DIRS})
	set(Zip_LIBRARIES ${ZIP_LIBRARIES})
endif()

find_package(LibSnap7 REQUIRED)
if(${LibSnap7_FOUND})
	set(SNAP_INCLUDE_DIRS ${LibSnap7_INCLUDEDIR})
	set(SNAP_LIBRARIES ${LibSnap7_LIBRARIES})
endif()

find_package(cppunit REQUIRED)
option(USE_XMLREADER "Use xmlTextReader instead of legacy XML DOM" ON)
find_package(LuaJIT REQUIRED)
if(${LUAJIT_FOUND})
  # TODO add support for compiling with LibLua if no LuaJIT found (as in autotools version)
  set(LUA_INCLUDE_DIR ${LUAJIT_INCLUDE_DIR})
  set(LUA_LIBRARIES ${LUAJIT_LIBRARIES})
else()
  set(NO_LUA true)
endif()
option(LUA_PARAM_OPTIMISE "Build with Lua param optimiser" ON)
find_package(FTGL REQUIRED)
if(${FTGL_FOUND})
  set(HAVE_FTGL true)
endif()
find_package(PAM REQUIRED)
find_package(wxWidgets REQUIRED COMPONENTS xrc html qa adv core xml net gl aui) #gtk)
find_package(GTK2 REQUIRED)
find_package(OpenGL REQUIRED)
find_package(SQLite3 REQUIRED)
if(${wxWidgets_FOUND})
  set(HAVE_GLCANVAS true)
endif()
find_package(CGAL QUIET)
if(NOT ${CGAL_FOUND})
  set(NO_CGAL true)
endif()
find_library(libnewt-dev newt REQUIRED)

find_program(NOSETESTS_PATH nosetests)

find_program(CCACHE_FOUND ccache)
if(CCACHE_FOUND)
	set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE ccache)
	set_property(GLOBAL PROPERTY RULE_LAUNCH_LINK ccache)
endif(CCACHE_FOUND)

# TRZEBA WSZYSTKO WCZESNIEJ SPRAWDIZC!
include_directories(${CMAKE_CURRENT_BINARY_DIR})
configure_file(config.h.cmake config.h)
